use chrono::Local;
use rusqlite:: {
    Connection,
    Result as SqliteResult
};

#[derive(Debug)]
struct Log {
    time: String,
    content: String,
    describe: String
}

// #[derive(Debug)]
// struct Student {
//     // 姓名
//     id: i32,
//     name: String,
//     // 年级
//     rank: i8,
//     class: i8,
//     // 宿舍
//     dormitory: i8,
//     dormitory_room: i8,
//     // 积分
//     credits: i32
// }

pub fn initialize_sql() -> SqliteResult<()> {
    let _ = std::fs::create_dir_all("./data");
    let conn = Connection::open("./data/data.db")?;

    conn.execute(
        "CREATE TABLE student (
            id INTEGER PRIMARY KEY,
            name TEXT NOT NULL,
            rank INTEGER NOT NULL,
            class INTEGER NOT NULL,
            dormitory INTEGER NOT NULL,
            dormitory_room INTEGER NOT NULL,
            credits REAL NOT NULL
        )",
        ()
    )?;

    conn.execute(
        "CREATE TABLE log (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            time TEXT NOT NULL,
            content TEXT NOT NULL,
            describe TEXT
        )", 
        ()
    )?;

    let log = Log {
        time: Local::now().format("%Y-%m-%d %H:%M:%S").to_string(),
        content: "初始化了数据库".to_string(),
        describe: "初始化数据库 测试消息！".to_string(),
    };

    conn.execute(
        "INSERT INTO log (time, content, describe) VALUES (?1, ?2, ?3)", 
        (&log.time, &log.content, &log.describe),
    )?;
    
    Ok(())
}